package com.twu.biblioteca.util;

import com.twu.biblioteca.Library;
import com.twu.biblioteca.user.UserInfo;

public class UserAccountValidator {
    public static final String NOT_LOG_IN_TIP = "Sorry, you have not logged in. Please log in first!";

    public UserInfo validate(Library library) {
        if(null == Library.currentUser) {
            library.showMessage(NOT_LOG_IN_TIP);
            Library.currentUser = getAndValidateUserInfo(library);
            getAndValidatePassword(library);
            Library.currentUser.setLoggedIn(true);
        }
        return Library.currentUser;
    }

    private UserInfo getAndValidateUserInfo(Library library) {
        library.showMessage("Input username:");
        String username = library.getMessage();
        while (null == UserInfo.from(username)) {
            library.showMessage("Username wrong! Please input username again:");
            username = library.getMessage();
        }
        return UserInfo.from(username);
    }

    private String getAndValidatePassword(Library library) {
        library.showMessage("Input password:");
        String password = library.getMessage();
        while (!Library.currentUser.getPassword().equals(password)) {
            library.showMessage("Password wrong! Please input password again:");
            password = library.getMessage();
        }
        return password;
    }

}


